package org.devocean.groupbuying.web.frontend;

import java.security.Principal;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.devocean.groupbuying.domain.Cart;
import org.devocean.groupbuying.domain.Customer;
import org.devocean.groupbuying.domain.Deal;
import org.devocean.groupbuying.service.CartService;
import org.devocean.groupbuying.service.CustomerService;
import org.devocean.groupbuying.service.DealService;
import org.devocean.groupbuying.utils.UriUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
// @RequestMapping(value = { "/{lang:[a-z]{2}}" })
@RequestMapping(value = { "/customers" })
public class CartController {
	@Autowired
	private CartService cartService;

	@Autowired
	private DealService dealService;

	@Autowired
	private CustomerService customerService;
	
	@Value("#{Path['path']}")
	private String path;

	// @RequestMapping(value = "buy/{id}")
	// public String addToCart(@PathVariable("id") Integer id, ModelMap
	// modelMap,
	// Principal p, @CookieValue("clientLanguage") String lang) {
	// modelMap.addAttribute(cartService.addToCart(id,
	// Integer.valueOf(p.getName()), lang));
	// return "customer/cart";
	// }

	@RequestMapping(value = "cart/{amount}")
	public String cart(ModelMap modelMap, Principal p,
			@PathVariable("amount") Double am) {
		Cart cart = cartService.getCurrentCart();
		cart.setAmount(am);
		modelMap.addAttribute(cartService.getCurrentCart());
		return "customer/cart";
	}

	@RequestMapping(value = "cartget")
	public String cart1(ModelMap modelMap, Principal p) {
		Cart cart = cartService.getCurrentCart();
		System.out.println(cart.getAmount());

		return "customer/cart";
	}

	@RequestMapping(value = "cartt")
	public String cart2(ModelMap modelMap, Principal p) {
		cartService.cleanCart();
		return "customer/cart";
	}

	@RequestMapping(value = "share", method = RequestMethod.POST)
	public String share(ModelMap modelMap, HttpServletRequest request,
			Principal p, @CookieValue("clientLanguage") String lang,
			Integer dealID) {
		cartService.cleanCart();
		final Enumeration e = request.getParameterNames();
		Customer customer = customerService.getCustomerById(Integer.valueOf(p
				.getName()));
		while (e.hasMoreElements()) {
			String param = (String) e.nextElement();
			if (param.contains("orders")) {
				if (Integer.valueOf(request.getParameter(param)) > 0) {
					cartService.addToCart(Integer.valueOf(param.split(":")[1]),
							Integer.valueOf(request.getParameter(param)),
							Integer.valueOf(p.getName()), lang);
				}
			}

		}

		Deal deal = dealService.getDealById(dealID);

		modelMap.addAttribute("title", deal.getProduct()
				.getArticlesLocalization().getArticles().iterator().next()
				.getTitle());
		modelMap.addAttribute("mediaFilePath", deal.getProduct()
				.getArticlesLocalization().getMediaLocalizations().iterator()
				.next().getMediaFilePath());
		if (request.getSession().getAttribute("customerUuid") != null) {
			String uuid = request.getSession().getAttribute("customerUuid")
					.toString();
			if (customer.getUuid().equals(uuid)) {
				request.getSession().setAttribute("customerUuid", null);
				
			}
		}

		String localPath = request.getContextPath() + "/" + lang + "/";
		String uri = path+ localPath + "deals/" + deal.getDealID() + "/share/"
				+ customer.getUuid();

		System.out.println(uri);
		modelMap.addAttribute("uri", uri);
		return "customer/share";
	}

	@RequestMapping(value = "purchase")
	public String purchase(HttpServletRequest request, Principal p,
			@CookieValue("clientLanguage") String lang) {
		return "redirect:payment/paypal/checkout";
	}

	@RequestMapping(value = "clearCart")
	public String clear(ModelMap modelMap, Principal p) {
		cartService.cleanCart();
		modelMap.addAttribute(cartService.getCurrentCart());
		return "customer/cart";
	}
}
